<?php

namespace Home\Controller;

use Think\Controller;
use Think\View;

class MatchController extends CommonController  //邹佳敏
{

    //显示所有比赛列表
    public function matchList()
    {

        $match = M('match');
        $this->matchList = $match->select();
        $cnt = 1;
        $this->assign('cnt', $cnt);
        $this->display();
    }

    //显示添加比赛信息界面
    public function matchAdd()
    {
        $this->display();
    }

    //添加比赛信息
    public function doMatchAdd()
    {
        $Match = D("match");

        if (!$Match->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Match->getError());
        } else {
            // 创建成功，做其他事情
            if ($Match->create()) {
                $pname = 'default.jpg';
                if ($picinfo = $this->upload()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $Match->match_photo = $pname;
            if ($Match->add()) {
                $this->success('添加成功', './matchList', 3);
            } else {
                $this->error("添加失败");
            }
        }
    }

    //删除比赛信息
    public function matchDel($matchId)
    {

        $Match = M('Match');

        $photoName = ($Match->select($matchId))['0']['match_photo'];
        if ($photoName != 'default.jpg') { //如果不是默认文件
            unlink('./Public/upload/matchpics/' . $photoName); //删除对应文件
        }
        if ($Match->delete($matchId)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //显示比赛详情+修改比赛信息
    public function matchMod($matchId)
    {

        $Match = M('Match');

        $this->matchInfo = $Match->select($matchId);
        $this->display();
    }

    //显示比赛详情+修改比赛信息
    public function doMatchMod($matchId)
    {

        $Match = D('Match');

        $matchInfo = $Match->select($matchId);

        if (!$Match->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Match->getError());
        } else {
            // 创建成功，做其他事情
            if ($Match->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/matchpics/' . $matchInfo['0']['match_photo']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Match->match_photo = $pname;
                }
                $Match->match_id = $matchId;
                if ($Match->save() !== false) {
                    $this->success('修改成功', './matchList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }


    //文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/matchpics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }

    public function interfaceMatch()
    {
        //$id = I('get.id');

        $match = M('match');
        $result = $match->select();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

    public function interfaceMatchInfo()
    {
        $id=I('get.match_id');
        $match=M('match');
        $result = $match->where("match_id=$id")->find();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

}